package com.openbravo.pos.panels;

import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import cn.ekuma.data.ui.swing.dnd.AbstractJBeanDropPanel;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.db.table.I_ProductLocationState;

import com.openbravo.bean.Location;
import com.openbravo.bean.ProductInfo;
import com.openbravo.bean.ProductLocationState;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.event.LocationChangedListener;

public class JDropProductByLocationStatePanel extends JPanel implements LocationChangedListener {
	I_DataLogicSales dlSales ;
	QBFParameters delQBFParameters;
	QBFParameter productParameter=new QBFParameter(I_ProductLocationState.PRODUCT_ID);
	QBFParameter locationLayoutParameter=new QBFParameter(I_ProductLocationState.LOCATION_ID);
	AbstractJBeanDropPanel<Location,ProductInfo> panel1,panel2,panel3,panel4;
	
	/**
	 * Create the panel.
	 */
	public JDropProductByLocationStatePanel(final I_DataLogicSales dlSales2) {
		initComponents();
		this.dlSales=dlSales2;
		delQBFParameters=new QBFParameters();
		delQBFParameters.and(productParameter).and(locationLayoutParameter);
		
		panel1=new AbstractJBeanDropPanel<Location,ProductInfo>(){
			@Override
			public void doDropInsert(List<ProductInfo> beans) {
						
				  for(ProductInfo p:beans){
					try {	
							dlSales.insertOrUpdateProductState(bean.getKey(),p.getID(),ProductLocationState.STATE_IN);
						} catch (BasicException e) {
							e.printStackTrace();
						}
					}
			}
			
			public void doDropDelete(List<ProductInfo> dropList) {
				locationLayoutParameter.equal(bean.getKey());
				for(ProductInfo p:dropList){			
					productParameter.equal(p.getID());
					try {
					   dlSales.deleteByQBFParameters(ProductLocationState.class, delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			} 

			@Override
			public String readerBean(Location obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.productLocationState.in");
			}
		};
		add(panel1);
		
		panel2=new AbstractJBeanDropPanel<Location,ProductInfo>(){
			@Override
			public void doDropInsert(List<ProductInfo> beans) {
						
				  for(ProductInfo p:beans){
					try {	
							dlSales.insertOrUpdateProductState(bean.getKey(),p.getID(),ProductLocationState.STATE_OUT);
						} catch (BasicException e) {
							e.printStackTrace();
						}
					}
			}
			
			public void doDropDelete(List<ProductInfo> dropList) {
				locationLayoutParameter.equal(bean.getKey());
				for(ProductInfo p:dropList){			
					productParameter.equal(p.getID());
					try {
					   dlSales.deleteByQBFParameters(ProductLocationState.class, delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			} 

			@Override
			public String readerBean(Location obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.productLocationState.out");
			}
		};
		add(panel2);
		
		panel3=new AbstractJBeanDropPanel<Location,ProductInfo>(){
			@Override
			public void doDropInsert(List<ProductInfo> beans) {
						
				  for(ProductInfo p:beans){
					try {	
							dlSales.insertOrUpdateProductState(bean.getKey(),p.getID(),ProductLocationState.STATE_LOCK);
						} catch (BasicException e) {
							e.printStackTrace();
						}
					}
			}
			
			public void doDropDelete(List<ProductInfo> dropList) {
				locationLayoutParameter.equal(bean.getKey());
				for(ProductInfo p:dropList){			
					productParameter.equal(p.getID());
					try {
					   dlSales.deleteByQBFParameters(ProductLocationState.class, delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			} 

			@Override
			public String readerBean(Location obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.productLocationState.lock");
			}
		};
		add(panel3);
		
	}
	private void initComponents() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
	}
	
	public static String getTabName(){
		return AppLocal.getIntString("label.productLocationState");
	}
	@Override
	public void valueSelectChanged(Location obj) {
		panel1.setBean(obj);
		panel2.setBean(obj);
		panel3.setBean(obj);
	}

}
